// console.log(__dirname);
const http = require('http')
const fs = require('fs')
const Stream = require('stream')

// const server = http.createServer((req, res) => {
//   res.setHeader('Content-Type', 'text/plain;CharSet=utf-8')
//   // fs.readFile(__dirname + '/text.txt', (err, data) => {
//   //   res.end(data)
//   // })

//   const stream = fs.createReadStream(__dirname + '/text.txt')
//   // 它获取来源流，并将其通过管道传输到目标流
//   // 文件流通过管道传输到 HTTP 响应
//   stream.pipe(res)
// })

// server.listen(3000)

// 创建可读流

// const readableStream = new Stream.Readable()
// readableStream._read = () => {}

const readableStream = new Stream.Readable({
  read(){ 
    console.log("read...")
   }
})

// 创建可写流
const writableStream = new Stream.Writable()
writableStream._write = (chunk, encoding, next) => {
  console.log(chunk);
  console.log(chunk.toString());
  next()
}

process.stdin.pipe(writableStream)

readableStream.push('hi')
readableStream.push('hoo')

// 从可读流中获取数据
readableStream.on("readable", () => {
  console.log(readableStream.read().toString());
})

writableStream.write("hey!")

writableStream.end()
